//
//  YSEMSAddTripViewController.m
//  YaSha-iOS
//
//  Created by 方鹏俊 on 2017/11/20.
//

#import "YSEMSAddTripViewController.h"
#import "YSFormRowModel.h"
#import "YSFormCommonCell.h"
#import "YSAreaPickerView.h"
#import "YSEMSProPageController.h"

@interface YSEMSAddTripViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) BOOL isForeign;
@property (nonatomic, assign) BOOL showProName;
@property (nonatomic, strong) NSArray *payloadArray;
@property (nonatomic, strong) NSMutableDictionary *tripRowDic;
@property (nonatomic, strong) NSMutableDictionary *payload;

@end

@implementation YSEMSAddTripViewController

static NSString *cellIdentifier = @"FormCommonCell";

- (NSArray *)payloadArray {
    if (!_payloadArray) {
        _payloadArray = @[@"businessArea", @"", @"", @"address", @"startTime", @"proType", @"proName", @"tripMode", @"buyTickets", @"bookHot al", @"remark"];
    }
    return _payloadArray;
}

- (void)initSubviews {
    [super initSubviews];
    self.title = @"添加行程";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[YSFormCommonCell class] forCellReuseIdentifier:cellIdentifier];
    [self.view addSubview:self.tableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedPro:) name:@"EMSSelectedPro" object:nil];
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self prepareDataSource];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
   
}
- (void)prepareDataSource {
    if (self.configArray.count) {//有数据，编辑情况
        for (YSFormRowModel *model in self.configArray) {

            model.disable = NO;
            model.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
             if ([model.title isEqualToString:@"预定酒店"]) {
                  model.rowName = @"YSFormSwitchCell";
                 
                  model.accessoryType = UITableViewCellAccessoryNone;
             }
            if ([model.title isEqualToString:@"出发日期"]) {//编辑时设置时间范围
                YSFormRowModel *emsStartDateModel = self.emsArr[0][4];
                YSFormRowModel *emsEndDateModel = self.emsArr[0][5];
                //设置时间范围
                model.minimumDate = [YSUtility dateFromString:emsStartDateModel.detailTitle andFormatter:@"yyyy-MM-dd"];
                model.maximumDate = [YSUtility dateFromString:emsEndDateModel.detailTitle andFormatter:@"yyyy-MM-dd"];
            }
//            //项目人员,项目类型,项目名称不能编辑
//            if ([model.title isEqualToString:@"项目类型"]) {
//                model.disable = _proListModel ? YES : NO;
//            }
//            if ([model.title isEqualToString:@"项目名称"]) {
//                model.disable = _proListModel ? YES : NO;
//            }
            
            if ([model.title isEqualToString:@"出行方式"]) {
                NSDictionary *tripModeDic = @{@"ccd_tripmode_fj": @"飞机", @"ccd_tripmode_hc": @"火车", @"ccd_tripmode_qc": @"汽车"};
                NSArray *tripModeArray = nil;
                if (model.detailTitle.length) {//选择过 行程
                    tripModeArray = [model.detailTitle componentsSeparatedByString:@","];
                }
                NSMutableSet *indexSets = [[NSMutableSet alloc]init];
                for (int i = 0; i < tripModeArray.count; i ++) {
                    NSString *key = tripModeArray[i];
                    //
                    if ([key isEqualToString:@"飞机"]) {
                        [indexSets addObject:@0];
                    }else if ([key isEqualToString:@"火车"]){
                        [indexSets addObject:@1];
                    }else if ([key isEqualToString:@"汽车"]){
                        [indexSets addObject:@2];
                    }
                }
                model.selectedItemIndexes = indexSets;
            }
            
             if ([model.title isEqualToString:@"交通代买"]) {
                 if (model.detailTitle.length) {//选择了交通代买，目前只有飞机票
                     NSMutableSet *indexSets = [[NSMutableSet alloc]init];
                     [indexSets addObject:@0];
                     model.selectedItemIndexes = indexSets;
                 }
             }
        }
        
        //备注
        YSFormRowModel *lastModel = [self.configArray lastObject];
        if ([lastModel.title isEqualToString:@"备注"]) {
            YSFormRowModel *remarkModel = [self.configArray lastObject];
            remarkModel.rowName = @"YSFormTextViewCell";//回去时将textview换掉
        }
        
        
    }else{//没数据，添加行程情况
       
        self.configArray = [NSMutableArray array];
        YSFormRowModel *model0 = [[YSFormRowModel alloc] init];
        model0.rowName = @"YSFormOptionsCell";
        model0.title = @"出差地区";
        model0.detailTitle = @"国内";//没数据默认国内
        model0.necessary = YES;
        model0.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model0.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        model0.optionsReturnType = YSFormOptionsReturnKey;
        model0.optionsDataArray = @[@{@"1": @"国内"},
                                    @{@"2": @"国外"}];
        [self.configArray addObject:model0];
        YSFormRowModel *model1 = [[YSFormRowModel alloc] init];
        model1.rowName = @"YSFormAreaPickerCell";
        model1.title = @"出发地";
        model1.necessary = YES;
        model1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model1.indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
        model1.areaPickerType = YSAreaPickerCity;
        [self.configArray addObject:model1];
        
        YSFormRowModel *model2 = [[YSFormRowModel alloc] init];
        model2.rowName = @"YSFormAreaPickerCell";
        model2.title = @"目的地";
        model2.necessary = YES;
        model2.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model2.indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        model2.areaPickerType = YSAreaPickerCity;
        [self.configArray addObject:model2];
        
        //        YSFormRowModel *model3 = [[YSFormRowModel alloc] init];
        //        model3.title = @"国外地址";
        //        model3.rowName = @"YSFormTextFieldCell";
        //        model3.indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
        
        
        YSFormRowModel *model4 = [[YSFormRowModel alloc] init];
        model4.rowName = @"YSFormDatePickerCell";
        model4.title = @"出发日期";
        YSFormRowModel *emsStartDateModel = self.emsArr[0][4];
        YSFormRowModel *emsEndDateModel = self.emsArr[0][5];
        
        model4.detailTitle = emsStartDateModel.detailTitle;
        model4.necessary = YES;
        model4.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model4.indexPath = [NSIndexPath indexPathForRow:4 inSection:0];
        model4.datePickerMode = PGDatePickerModeDate;
        //设置时间范围
        model4.minimumDate = [YSUtility dateFromString:emsStartDateModel.detailTitle andFormatter:@"yyyy-MM-dd"];
        model4.maximumDate = [YSUtility dateFromString:emsEndDateModel.detailTitle andFormatter:@"yyyy-MM-dd"];
        [self.configArray addObject:model4];
        
        YSFormRowModel *model5 = [[YSFormRowModel alloc] init];
        model5.rowName = @"YSFormOptionsCell";
        model5.title = @"项目类型";
        model5.detailTitle = _proListModel ? @"项目" : @"空";
//        if (_proListModel) {
//            [self.tripRowDic setValue:@"ccd_protype_xm" forKey:@"proType"];
//            [self.tripRowDic setValue:_proListModel.name forKey:@"proName"];
//            [self.payload setValue:@"ccd_protype_xm" forKey:@"proType"];
//            [self.payload setValue:_proListModel.name forKey:@"proName"];
//            [self.payload setValue:_proListModel.code forKey:@"proCode"];
//            [self.payload setValue:_proListModel.id forKey:@"proId"];
//        }
        model5.necessary = YES;
        model5.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        model5.indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
        model5.optionsDataArray = @[@{@"ccd_protype_k": @"空"},
                                    @{@"ccd_protype_xm": @"项目"},
                                    @{@"ccd_protype_yx": @"营销"}];
        model5.disable = _proListModel ? YES : NO;
        [self.configArray addObject:model5];
        if (_proListModel) {//有项目显示项目名称
            YSFormRowModel *model6 = [[YSFormRowModel alloc] init];
            model6.proListModel = _proListModel;
            model6.rowName = @"YSFormJumpCell";
            model6.jumpClass = @"YSEMSProPageController";
            model6.title = @"项目名称";
            model6.detailTitle = _proListModel.name;
            model6.necessary = YES;
            model6.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            model6.indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
           // model6.disable = _proListModel ? YES : NO;
            [self.configArray addObject:model6];
        }
       
        
        YSFormRowModel *model7 = [[YSFormRowModel alloc] init];
        model7.rowName = @"YSFormMultipleSelectCell";
        model7.title = @"出行方式";
        model7.indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
        model7.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model7.optionsDataArray = @[@{@"ccd_tripmode_fj": @"飞机"},
                                    @{@"ccd_tripmode_hc": @"火车"},
                                    @{@"ccd_tripmode_qc": @"汽车"}];
        model7.optionsReturnType = YSFormOptionsReturnKey;
        [self.configArray addObject:model7];
        
        YSFormRowModel *model8 = [[YSFormRowModel alloc] init];
        model8.rowName = @"YSFormMultipleSelectCell";
        model8.title = @"交通代买";
        model8.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        model8.indexPath = [NSIndexPath indexPathForRow:8 inSection:0];
        model8.optionsDataArray = @[@{@"ccd_buytickets_fjp": @"飞机票"}];
        model8.optionsReturnType = YSFormOptionsReturnKey;
        [self.configArray addObject:model8];
        
        YSFormRowModel *model9 = [[YSFormRowModel alloc] init];
        model9.rowName = @"YSFormSwitchCell";
        model9.title = @"预定酒店";
        model9.indexPath = [NSIndexPath indexPathForRow:9 inSection:0];
        [self.configArray addObject:model9];
        
        YSFormRowModel *model10 = [[YSFormRowModel alloc] init];
        model10.rowName = @"YSFormTextViewCell";
        model10.title = @"备注";
        model10.placeholder = @"如有特殊情况请备注";
        model10.indexPath = [NSIndexPath indexPathForRow:10 inSection:0];
        model10.maximumTextLength = 50;
        [self.configArray addObject:model10];
        
    }
    [self.tableView reloadData];
}

#pragma mark - 选择完项目回调
- (void)selectedPro:(NSNotification *)notification {
    UIViewController *vc =  [YSUtility getCurrentViewController];
   UIViewController *otherVC  = [vc.navigationController.viewControllers objectAtIndex:vc.navigationController.viewControllers.count - 2];
    if (![otherVC.title isEqualToString:@"添加行程"]) {
        return;
    }
  
    YSEMSProListModel *cellModel = notification.userInfo[@"proModel"];
    YSFormRowModel *model6 = self.configArray[_isForeign ? 4 : 5];
    model6.proListModel = cellModel;
    model6.detailTitle = cellModel.name;
    [self.tableView reloadData];
    //[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_isForeign ? 4 : 5 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];//不知道为什么会偶然崩溃
}

- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
    self.navigationItem.rightBarButtonItem = [QMUINavigationButton barButtonItemWithType:QMUINavigationButtonTypeNormal title:@"完成" position:QMUINavigationButtonPositionRight target:self action:@selector(submit)];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.configArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSFormRowModel *cellModel = _configArray[indexPath.row];
    YSFormCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[NSClassFromString(cellModel.rowName) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    [cell setCellModel:cellModel];//选择的值记在model里
    //国内、出发日期，项目类型，出行方式，交通代买，预定酒店，备注，国外地址
    YSWeak;
    [cell.sendFormCellModelSubject subscribeNext:^(YSFormCellModel *model) {
        YSStrong;
        [strongSelf.payload setObject:model.value forKey:strongSelf.payloadArray[model.indexPath.row]];
        if (model.indexPath.row == 0) {//选择国内还是国外
            strongSelf.isForeign = [model.value isEqual:@"1"] ? NO : YES;
            if (strongSelf.isForeign) {
                NSMutableArray *deleteArray = [NSMutableArray array];
                NSMutableArray *deleteIndexPathArray = [NSMutableArray array];
                for (YSFormRowModel *formRowModel in weakSelf.configArray) {
                    if ([formRowModel.title isEqual:@"出发地"] || [formRowModel.title isEqual:@"目的地"]) {
                        [deleteArray addObject:formRowModel];
                        NSUInteger index = [strongSelf.configArray indexOfObject:formRowModel];
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
                        [deleteIndexPathArray addObject:indexPath];
                    }
                }
                [strongSelf.configArray removeObjectsInArray:[deleteArray copy]];
                [strongSelf.tableView deleteRowsAtIndexPaths:[deleteIndexPathArray copy] withRowAnimation:UITableViewRowAnimationAutomatic];
                
                YSFormRowModel *formRowModel = [strongSelf.configArray objectAtIndex:1];
                if (![formRowModel.title isEqual:@"国外地址"]) {
                    YSFormRowModel *model3 = [[YSFormRowModel alloc] init];
                    model3.title = @"国外地址";
                    model3.placeholder = @"请输入国外地址";
                    model3.rowName = @"YSFormTextFieldCell";
                    model3.necessary = YES;
                    model3.indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
                    
                    [strongSelf.configArray insertObject:model3 atIndex:1];
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
                    [strongSelf.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                }
                [strongSelf.payload setValue:@"" forKey:@"startAddress"];
                [strongSelf.payload setValue:@"" forKey:@"endAddress"];
            } else {
                NSMutableArray *deleteArray = [NSMutableArray array];
                NSMutableArray *deleteIndexPathArray = [NSMutableArray array];
                for (YSFormRowModel *formRowModel in strongSelf.configArray) {
                    if ([formRowModel.title isEqual:@"国外地址"]) {
                        [deleteArray addObject:formRowModel];
                        NSUInteger index = [strongSelf.configArray indexOfObject:formRowModel];
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
                        [deleteIndexPathArray addObject:indexPath];
                    }
                }
                [strongSelf.configArray removeObjectsInArray:[deleteArray copy]];
                [strongSelf.tableView deleteRowsAtIndexPaths:[deleteIndexPathArray copy] withRowAnimation:UITableViewRowAnimationAutomatic];
                YSFormRowModel *formRowModel = [strongSelf.configArray objectAtIndex:1];
                if (![formRowModel.title isEqual:@"出发地"]) {//如果已经选择国内，再次选择时不创建
                    
                    YSFormRowModel *model1 = [[YSFormRowModel alloc] init];
                    model1.title = @"出发地";
                    model1.rowName = @"YSFormAreaPickerCell";
                    model1.necessary = YES;
                    model1.indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
                    model1.areaPickerType = YSAreaPickerCity;
                    model1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    
                    YSFormRowModel *model2 = [[YSFormRowModel alloc] init];
                    model2.title = @"目的地";
                    model2.rowName = @"YSFormAreaPickerCell";
                    model2.necessary = YES;
                    model2.indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
                    model2.areaPickerType = YSAreaPickerCity;
                    model2.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    
                    [strongSelf.configArray insertObject:model1 atIndex:1];
                    [strongSelf.configArray insertObject:model2 atIndex:2];
                    NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:1 inSection:0];
                    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:2 inSection:0];
                    [strongSelf.tableView insertRowsAtIndexPaths:@[indexPath0, indexPath1] withRowAnimation:UITableViewRowAnimationAutomatic];
                }
            }
            [strongSelf.payload setValue:@"" forKey:@"foreignAddress"];
            /** 处理项目类型 */
        } else if (model.indexPath.row == 5) {//项目类型
            if ([model.value isEqual:@"ccd_protype_xm"]) {//选择项目
                NSMutableArray *deleteArray = [NSMutableArray array];
                NSMutableArray *deleteIndexPathArray = [NSMutableArray array];
                for (YSFormRowModel *formRowModel in strongSelf.configArray) {
                    if ([formRowModel.title isEqual:@"项目名称"]) {
                        [deleteArray addObject:formRowModel];
                        NSUInteger index = [strongSelf.configArray indexOfObject:formRowModel];
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
                        [deleteIndexPathArray addObject:indexPath];
                    }
                }
                [strongSelf.configArray removeObjectsInArray:[deleteArray copy]];
                [strongSelf.tableView beginUpdates];
                [strongSelf.tableView deleteRowsAtIndexPaths:[deleteIndexPathArray copy] withRowAnimation:UITableViewRowAnimationAutomatic];
                [strongSelf.tableView endUpdates];
                
                YSFormRowModel *model6 = [[YSFormRowModel alloc] init];
                model6.title = @"项目名称";
                model6.rowName = @"YSFormJumpCell";
                model6.necessary = YES;
                model6.indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
                model6.jumpClass = @"YSEMSProPageController";
                
                [strongSelf.configArray insertObject:model6 atIndex:strongSelf.isForeign ? 4 : 5];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:strongSelf.isForeign ? 4 : 5 inSection:0];
                 [strongSelf.tableView beginUpdates];
                [strongSelf.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                 [strongSelf.tableView endUpdates];
            } else if ([model.value isEqual:@"ccd_protype_yx"]) {
                NSMutableArray *deleteArray = [NSMutableArray array];
                NSMutableArray *deleteIndexPathArray = [NSMutableArray array];
                for (YSFormRowModel *formRowModel in strongSelf.configArray) {
                    if ([formRowModel.title isEqual:@"项目名称"]) {
                        [deleteArray addObject:formRowModel];
                        NSUInteger index = [strongSelf.configArray indexOfObject:formRowModel];
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
                        [deleteIndexPathArray addObject:indexPath];
                    }
                }
                [strongSelf.configArray removeObjectsInArray:[deleteArray copy]];
                [strongSelf.tableView beginUpdates];
                [strongSelf.tableView deleteRowsAtIndexPaths:[deleteIndexPathArray copy] withRowAnimation:UITableViewRowAnimationAutomatic];
                [strongSelf.tableView endUpdates];
                
                YSFormRowModel *model6 = [[YSFormRowModel alloc] init];
                model6.title = @"项目名称";
                model6.rowName = @"YSFormTextFieldCell";
                model6.necessary = YES;
                model6.indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
                
                [weakSelf.configArray insertObject:model6 atIndex:strongSelf.isForeign ? 4 : 5];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:strongSelf.isForeign ? 4 : 5 inSection:0];
                 [weakSelf.tableView beginUpdates];
                [weakSelf.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                 [weakSelf.tableView endUpdates];
            } else {
                NSMutableArray *deleteArray = [NSMutableArray array];
                NSMutableArray *deleteIndexPathArray = [NSMutableArray array];
                for (YSFormRowModel *formRowModel in strongSelf.configArray) {
                    if ([formRowModel.title isEqual:@"项目名称"]) {
                        [deleteArray addObject:formRowModel];
                        NSUInteger index = [strongSelf.configArray indexOfObject:formRowModel];
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
                        [deleteIndexPathArray addObject:indexPath];
                    }
                }
                [strongSelf.configArray removeObjectsInArray:[deleteArray copy]];
                [strongSelf.tableView beginUpdates];
                [strongSelf.tableView deleteRowsAtIndexPaths:[deleteIndexPathArray copy] withRowAnimation:UITableViewRowAnimationAutomatic];
                 [strongSelf.tableView endUpdates];
            }
            [strongSelf.payload setValue:@"" forKey:@"proName"];
 }
    }];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (void)submit {
    //校验数据完整性
    /** 校验数据完整 */
    NSMutableDictionary *detailDic = [NSMutableDictionary dictionary];
    YSFormRowModel *addresModel = self.configArray[0];

    if ([addresModel.detailTitle isEqualToString:@"国内"]) {
        YSFormRowModel *startAreaModel = self.configArray[1];
        YSFormRowModel *destiModel = self.configArray[2];
        [detailDic setValue:@"1" forKey:@"businessArea"];//国内
        [detailDic setValue:startAreaModel.addressModel.province forKey:@"startProvince"];
        [detailDic setValue:startAreaModel.addressModel.provinceCode forKey:@"startProvinceCode"];
        [detailDic setValue:startAreaModel.addressModel.city forKey:@"startCity"];
        [detailDic setValue:startAreaModel.addressModel.cityCode forKey:@"startCityCode"];
        [detailDic setValue:startAreaModel.addressModel.area forKey:@"startArea"];
        [detailDic setValue:startAreaModel.addressModel.areaCode forKey:@"startAreaCode"];
        [detailDic setValue:destiModel.addressModel.province forKey:@"endProvince"];
        [detailDic setValue:destiModel.addressModel.provinceCode forKey:@"endProvinceCode"];
        [detailDic setValue:destiModel.addressModel.city forKey:@"endCity"];
        [detailDic setValue:destiModel.addressModel.cityCode forKey:@"endCityCode"];
        [detailDic setValue:destiModel.addressModel.area forKey:@"endArea"];
        [detailDic setValue:destiModel.addressModel.areaCode forKey:@"endAreaCode"];
        
    }else{
        YSFormRowModel *destiModel = self.configArray[1];
        [detailDic setValue:@"2" forKey:@"businessArea"];//国外
       [detailDic setValue:destiModel.detailTitle forKey:@"address"];
        //[detailDic setValue:destiModel.detailTitle forKey:@"foreignAddress"];
        
    }
    //出发日期
    NSInteger index = [addresModel.detailTitle isEqualToString:@"国内"] ? 3 :2;
    YSFormRowModel *startTimeModel = self.configArray[index];
    [detailDic setValue:startTimeModel.detailTitle forKey:@"startTime"];
   
    //项目类型和项目名称
    for (YSFormRowModel *model in self.configArray) {//对于必填的用for找出来
        if ([model.title isEqualToString:@"项目类型"]) {
            if ([model.detailTitle isEqualToString:@"空"]) {
            [detailDic setValue:@"ccd_protype_k" forKey:@"proType"];
            }else if ([model.detailTitle isEqualToString:@"项目"]){
                [detailDic setValue:@"ccd_protype_xm" forKey:@"proType"];
                YSFormRowModel *model6 = self.configArray[_isForeign ? 4 : 5];
                [detailDic setValue:model6.proListModel.name forKey:@"proName"];
                [detailDic setValue:model6.proListModel.code forKey:@"proCode"];
                [detailDic setValue:model6.proListModel.id forKey:@"proId"];
            }else if ([model.detailTitle isEqualToString:@"营销"]) {
                [detailDic setValue:@"ccd_protype_yx" forKey:@"proType"];
                YSFormRowModel *model6 = self.configArray[_isForeign ? 4 : 5];
                [detailDic setValue:model6.detailTitle forKey:@"proName"];
            }
        }
    }
    
   //出行方式
    NSDictionary *tripModeDic = @{@"飞机": @"ccd_tripmode_fj", @"火车": @"ccd_tripmode_hc", @"汽车": @"ccd_tripmode_qc"};
    for (YSFormRowModel *model in self.configArray ) {
        if ([model.title isEqualToString:@"出行方式"]) {
            
            NSArray *tripModelArr = [model.detailTitle componentsSeparatedByString:@","];
            NSMutableArray *paraTrip = [NSMutableArray array];
            NSMutableString *paraStr = [[NSMutableString alloc]init];
            for (NSString *str in tripModelArr) {
               [paraTrip addObject:tripModeDic[str]];
            }
            //配置上传参数
            for (int i = 0; i < paraTrip.count; i ++) {
                if (i == 0) {
                    [paraStr appendString:paraTrip[i]];
                }else{
                    [paraStr appendString:[NSString stringWithFormat:@",%@",paraTrip[i]]];
                }
            }
            [detailDic setValue:paraStr forKey:@"tripMode"];
            
        }
}
    
    //交通代买
    for (YSFormRowModel *model in self.configArray ) {
        if ([model.title isEqualToString:@"交通代买"] && model.detailTitle.length) {
             [detailDic setValue:@"ccd_buytickets_fjp" forKey:@"buyTickets"];
            }
        
    }
    
//预定酒店，顺便将cell的类型换一下
for (YSFormRowModel *model in self.configArray ) {
    if ([model.title isEqualToString:@"预定酒店"]) {
        if (!model.switchStatusStr.length) {
            [detailDic setValue:@(0) forKey:@"bookHotal"];
             model.detailTitle = @"否";
        }else{
            [detailDic setValue:@(1) forKey:@"bookHotal"];
             model.detailTitle = model.switchStatusStr;
        }
         model.rowName = @"YSFormDetailCell";
    }
}
    
    
    
    //备注
     YSFormRowModel *lastModel = [self.configArray lastObject];
    if ([lastModel.title isEqualToString:@"备注"]) {
        YSFormRowModel *remarkModel = [self.configArray lastObject];
        remarkModel.detailTitle = remarkModel.remark;
        [detailDic setValue:remarkModel.remark forKey:@"remark"];
        if (!remarkModel.remark.length) {
            [self.configArray removeObject:remarkModel];
        }else{
            remarkModel.rowName = @"YSFormDetailCell";//回去时将textview换掉
        }
    }
    
   //校验
    if ([addresModel.detailTitle isEqualToString:@"国外"]) {
        
        if (!detailDic[@"address"] || !detailDic[@"startTime"]) {
            [QMUITips showError:@"请填写完整" inView:self.view hideAfterDelay:1];
            return;
        }
    } else {//国内
        if (!detailDic[@"startProvince"] || !detailDic[@"startCity"] || !detailDic[@"endProvince"] || !detailDic[@"endCity"] || !detailDic[@"startTime"]) {
            [QMUITips showError:@"请填写完整" inView:self.view hideAfterDelay:1];
            return;
        }
    }
    
    if (![detailDic[@"proType"] isEqualToString:@"ccd_protype_k"]) {
        if (!detailDic[@"proName"]) {
            [QMUITips showError:@"请填写完整" inView:self.view hideAfterDelay:1];
            return;
        }
    }
   
    
   
    
    
    
    if (self.isEditing) {//是编辑状态，改动已放在configArray里了,只需刷新相应的区
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
        NSInteger index = self.editingIndexPath.section;
        YSFormRowModel *model0 = [[YSFormRowModel alloc] init];
        model0.title = [NSString stringWithFormat:@"行程%@",[formatter stringFromNumber:[NSNumber numberWithInt:index]]];
        model0.rowName = @"YSFormDeleteCell";
        model0.indexPath = [NSIndexPath indexPathForRow:0 inSection:[_orderNo integerValue]];
        [self.configArray insertObject:model0 atIndex:0];
        
        [self.emsArr replaceObjectAtIndex:self.editingIndexPath.section withObject:self.configArray];
    }else{//添加状态
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
        NSInteger index = self.emsArr.count;
        YSFormRowModel *model0 = [[YSFormRowModel alloc] init];
        model0.title = [NSString stringWithFormat:@"行程%@",[formatter stringFromNumber:[NSNumber numberWithInt:index]]];
        model0.rowName = @"YSFormDeleteCell";
        model0.indexPath = [NSIndexPath indexPathForRow:0 inSection:[_orderNo integerValue]];
        [self.configArray insertObject:model0 atIndex:0];
        [self.emsArr addObject:self.configArray];
    }
    
    //通知传过去的内容
    NSMutableDictionary *trip = [NSMutableDictionary dictionary];
    [trip setValue:@(self.isEditing) forKey:@"isEditing"];
    [trip setValue:self.editingIndexPath forKey:@"indexPath"];
    [trip setValue:detailDic forKey:@"payload"];
    [trip setValue:[detailDic[@"buyTickets"] length] ? @"1" : @"0" forKey:@"buyTickets"];
    [trip setValue: self.configArray forKey:@"rowDataArr"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddTripNotification" object:nil userInfo:@{@"trip":trip}];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
